<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/28
 * Time: 16:16
 */

namespace app\admin\model;
use PDOException;
use think\Model;
use think\Db;
class UsersModel extends Model
{
    // 确定链接表名
    protected $table = 'snake_users';
    /**
     * 根据搜索条件获取用户列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getUsersByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('reg_time desc,user_id DESC')->select();
    }
    /**
     * 根据搜索条件获取所有的用户数量
     * @param $where
     */
    public function getAllUsers($where)
    {
        return $this->where($where)->count();
    }
    //获取投诉
    public function getComplaint($where, $offset, $limit){
        return Db::name('feedback')->alias('f')->field("f.*,car_no,title,car.realname,users.mobile,users.nickname,cu.mobile AS cMobile,car.mobile AS carMobile,users.nickname AS cNickname,cu.status AS cStatus")
            ->join('__USERS__ users','f.user_id = users.user_id','LEFT')
            ->join('__USERS__ cu','f.complain_user_id = cu.user_id','LEFT')
            ->join('__CAR__ car','f.car_id = car.car_id','LEFT')
            ->where($where)
            ->where("f.status=1 AND f.type=1")
            ->limit($offset, $limit)
            ->order("f.addtime DESC,f.id DESC")
            ->select();
    }

    //获取投诉总量
    public function getAllComplaint($where){
        return Db::name('feedback')->alias('f')
            ->join('__USERS__ users','f.user_id = users.user_id','LEFT')
            ->join('__USERS__ cu','f.complain_user_id = cu.user_id','LEFT')
            ->join('__CAR__ car','f.car_id = car.car_id','LEFT')
            ->where($where)
            ->where("f.status=1 AND f.type=1")
            ->count();
    }

    //获取反馈
    public function getFeedback($where, $offset, $limit){
        return Db::name('feedback')->alias('f')->field("f.*,users.mobile,users.nickname")
            ->join('__USERS__ users','f.user_id = users.user_id','LEFT')
            ->where($where)
            ->where("f.status=1 AND f.type=2")
            ->limit($offset, $limit)
            ->order("f.addtime DESC,f.id DESC")
            ->select();
    }
    public function getAllFeedback($where){
        return Db::name('feedback')->alias('f')->field("f.*,users.mobile,users.nickname")
            ->join('__USERS__ users','f.user_id = users.user_id','LEFT')
            ->where($where)
            ->where("f.status=1 AND f.type=2")
            ->count();
    }

    //获取日志
    public function getLog($where, $offset, $limit){
        return Db::name('user_log')->alias('f')->field("f.*,users.mobile,users.nickname")
            ->join('__USERS__ users','f.user_id = users.user_id','LEFT')
            ->where($where)
            ->limit($offset, $limit)
            ->order("f.log_time DESC,f.log_id DESC")
            ->select();
    }
    public function getAllLog($where){
        return Db::name('user_log')->alias('f')->field("f.*,users.mobile,users.nickname")
            ->join('__USERS__ users','f.user_id = users.user_id','LEFT')
            ->where($where)
            ->count();
    }
    public function getOneUser($id)
    {
        return $this->where('user_id', $id)->find();
    }
    public function getDetail($id){
        $user=$this->where('user_id', $id)->find();
        $user['has_level_detail']=0;
        if(!empty($user)){
            $user['level_tag'] ='LV'.$user['level'].' '.level_tag($user['level']);
            $user['underling_number'] =Db::name('users')->where(array('first_leader'=>$user['user_id']))->count();
            $user['has_level_detail']=0;
            $levelDetail=Db::name('user_level')->where(array('level'=>$user['level']))->find();
            if(!empty($levelDetail)){
                $user['has_level_detail']=1;
                $user['level_detail']=$levelDetail;
            }
        }
        return $user;
    }
    /**
     * 删除用户
     * @param $id
     */
    public function delUser($id)
    {
        try{

            $this->where('user_id', $id)->delete();
            return msg(1, '', '删除用户成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }
    /**
     * 编辑管理员信息
     * @param $param
     */
    public function editUser($param)
    {
        try{

            $result =  $this->save($param, ['user_id' => $param['id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                if(isset($param['status']) && $param['status']>1){
                    if($param['status']==2){
                        vendor("jpush.jpush");
                        $jpush = new \jpush();
                        $jpush->send('您的账号被平台禁用',$param['id']);
                    }elseif($param['status']==3){
                        vendor("jpush.jpush");
                        $jpush = new \jpush();
                        $jpush->send('您的账号被平台禁用一个月',$param['id']);
                    }
                }
                $url=url('users/index');
                if(isset($param['reurl'])&&$param['reurl']!='') $url=$param['reurl'];
                return msg(1, $url, '编辑用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    function getAllCert($map){
        if(!paramCheck($map,'type')) $map['type']=0;
        return Db::name('user_idcert')->where($map)->count();
    }

    function getCert($map,$offset,$limit){
        if(!paramCheck($map,'type')) $map['type']=0;
        $result=Db::name('user_idcert')->where($map)->limit("$offset , $limit")->order('id DESC')->select();
        return $result;
    }

    public function passCert($id){
        try{
            $cert=Db::name('user_idcert')->field('user_id,type')->where(array('id'=>$id))->find();
            if(empty($cert)) return msg(-1, '', '获取认证信息出错');
            $user_id=$cert['user_id'];

            $result =  Db::name('user_idcert')->where(array('id'=>$id))->update(array('status'=>1));
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                if($cert['type']==1){
                    Db::name('recycleman')->where(array('user_id'=>$user_id))->update(array('is_cert'=>1));
                    return msg(1, url('cert/index',array('role'=>1)), '审核成功');
                }else{
                    Db::name('users')->where(array('user_id'=>$user_id))->update(array('is_cert'=>1));
                    return msg(1, url('cert/index'), '审核成功');
                }
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function rejectCert($id){
        try{
            $cert=Db::name('user_idcert')->field('user_id,type')->where(array('id'=>$id))->find();
            if(empty($cert)) return msg(-1, '', '获取认证信息出错');
            $user_id=$cert['user_id'];
            $result =  Db::name('user_idcert')->where(array('id'=>$id))->update(array('status'=>-1));
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                if($cert['type']==1) {
                    Db::name('recycleman')->where(array('user_id' => $user_id))->update(array('is_cert' => -1));
                    return msg(1, url('cert/index',array('role'=>1)), '驳回成功');
                }else{
                    Db::name('users')->where(array('user_id' => $user_id))->update(array('is_cert' => -1));
                    return msg(1, url('cert/index'), '驳回成功');
                }
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
}